//
//  OrderViewCell.m
//  Peso Guide
//
//  Created by macbook on 2025/6/11.
//

#import "OrderViewCell.h"
#import "CustomWebViewController.h"
#import "ProductDetailViewController.h"
#import "MineSettingViewController.h"
#import "OrderViewContentCell.h"
#import "CustomWebViewController.h"

@interface OrderViewCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *iconImg;
@property (nonatomic, strong) UILabel *titLabel;
@property (nonatomic, strong) UIButton *loanButton;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIButton *applyButton;
@property (nonatomic, strong) UIView *centerView;
@property (nonatomic, strong) BaseTableView *contentTableView;

@end

@implementation OrderViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _backView = [PesoKit createViewWithFrame:CGRectMake(16, 16, KScreenWidth-32, 212) backGroundColor:ColorHex(@"#FFE7B4")];
        _backView.clipsToBounds = YES;
        _backView.layer.cornerRadius = 15.0f;
        [self.contentView addSubview:_backView];
        
        _centerView = [PesoKit createViewWithFrame:CGRectMake(17, 88, _backView.width-34, 0) backGroundColor:ColorHex(@"#FFFFFF")];
        _centerView.clipsToBounds = YES;
        _centerView.layer.cornerRadius = 10.0f;
        [_backView addSubview:_centerView];
        
        _iconImg = [PesoKit createImageViewWithFrame:CGRectMake(16, 16, 20, 20) imageNamed:@""];
        [_backView addSubview:_iconImg];
        
        _titLabel = [PesoKit createLabelWithFrame:CGRectMake(42, 14, 300, 22) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16)];
        [_backView addSubview:_titLabel];
        
        _loanButton = [PesoKit createButtonWithFrame:CGRectMake(_backView.width-18-200, 16, 200, 20) backGroundColor:[UIColor clearColor] title:[Lang set:@"Loan agreement >"] titleColor:ColorHex(@"#1A7A35") font:FONT(FontWeightStyleRegular, 14)];
        [_loanButton addTarget:self action:@selector(loanButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _loanButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_backView addSubview:_loanButton];
        
        _priceLabel = [PesoKit createLabelWithFrame:CGRectMake(18, 48, 300, 34) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleMedium, 24)];
        [_backView addSubview:_priceLabel];
        
        _applyButton = [PesoKit createButtonWithFrame:CGRectMake(_backView.width-16-90, 49, 90, 26) backGroundColor:ColorHex(@"#FE7133") title:[Lang set:@"Apply now"] titleColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleRegular, 14)];
        [_applyButton addTarget:self action:@selector(applyButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _applyButton.clipsToBounds = YES;
        _applyButton.layer.cornerRadius = 13.0f;
        [_backView addSubview:_applyButton];
        
        _contentTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, _backView.width-34, 0) style:UITableViewStyleGrouped];
        _contentTableView.backgroundColor = ColorHex(@"#FFFFFF");
        _contentTableView.delegate = self;
        _contentTableView.dataSource = self;
        _contentTableView.showsVerticalScrollIndicator = NO;
        _contentTableView.showsHorizontalScrollIndicator = NO;
        _contentTableView.scrollEnabled = NO;
        [_centerView addSubview:_contentTableView];
    }
    return self;
}

- (void)loanButtonClick {
    DLog(@"loadn");
    
    NSString *ordered = [NSString stringWithFormat:@"%@",[_dataDic safeObjectForKey:@"ordered"]];
    
    CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
    webVC.linkUrl = ordered;
    [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
}

- (void)applyButtonClick {
    DLog(@"apply");
    
    NSString *recent = [NSString stringWithFormat:@"%@",[_dataDic safeObjectForKey:@"survives"]];
    
    if ([recent rangeOfString:@"http://"].location != NSNotFound ||
        [recent rangeOfString:@"https://"].location != NSNotFound) {
        //webView
        CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
        webVC.linkUrl = recent;
        [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/thelike"].location != NSNotFound) {
        //设置
        MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
        settingVC.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
        return;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/andlove"].location != NSNotFound) {
        //首页
        [AppTools getCurrentVC].tabBarController.selectedIndex = 0;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/standard"].location != NSNotFound) {
        //登录
        [AppTools toLogin];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/classes"].location != NSNotFound) {
        //订单
        [AppTools getCurrentVC].tabBarController.selectedIndex = 1;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/islove"].location != NSNotFound) {
        //产品详情
        NSArray *array = [recent componentsSeparatedByString:@"="];
        NSString *theId = [NSString stringWithFormat:@"%@",[array safeObjectAtIndex:1]];
        ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
        vc.productId = theId;
        vc.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:vc animated:YES];
        return;
    }
}

- (void)setDataDic:(NSDictionary *)dataDic {
    _dataDic = dataDic;
    
    NSMutableArray *performed = [NSMutableArray arrayWithArray:[dataDic safeObjectForKey:@"performed"]];
    NSString *raised = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"raised"]];
    NSString *allowed = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"allowed"]];
    NSString *simpler = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"simpler"]];
    NSString *still = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"still"]];
    NSString *assigning = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"assigning"]];

    [_applyButton setTitle:assigning forState:0];
    CGSize applySize =  [AppTools sizeWithText:assigning font:FONT(FontWeightStyleRegular, 14) maxSize:CGSizeMake(200, 26)];
    _applyButton.width = ceil(applySize.width)+15;
    _applyButton.left = _backView.width-16-(ceil(applySize.width)+15);
    
    [_iconImg sd_setImageWithURL:[NSURL URLWithString:raised]];
    _titLabel.text = allowed;
    if ([AppTools isEmpty:simpler]) {
        _loanButton.hidden = YES;
    }else {
        _loanButton.hidden = NO;
        [_loanButton setTitle:simpler forState:0];
//        CGSize loanButtonSize = [AppTools sizeWithText:simpler font:FONT(FontWeightStyleRegular, 14) maxSize:CGSizeMake(500, 20)];
//        _loanButton.width = ceil(loanButtonSize.width)+10;
    }
    _priceLabel.text = still;
    
    if (performed.count > 0) {
        _centerView.height = 28*performed.count;
        _backView.height = 100+28*performed.count;
        _contentTableView.height = 28*performed.count;
        [_contentTableView reloadData];
    }
}

#pragma mark - UITableViewDelegate&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *performed = [NSMutableArray arrayWithArray:[_dataDic safeObjectForKey:@"performed"]];
    return performed.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *OrderViewContentCellIdentifier = [NSString stringWithFormat:@"OrderViewContentCellIdentifier%ld",(long)indexPath.row];
    OrderViewContentCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderViewContentCellIdentifier];
    if (!cell) {
        cell = [[OrderViewContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:OrderViewContentCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_dataDic != nil && _dataDic.allKeys.count > 0) {
        NSMutableArray *performed = [NSMutableArray arrayWithArray:[_dataDic safeObjectForKey:@"performed"]];
        if (performed.count > 0) {
            cell.dataDic = [performed safeObjectAtIndex:indexPath.row];
        }
    }
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 28;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _backView.width-34, 0.01)];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *recent = [NSString stringWithFormat:@"%@",[_dataDic safeObjectForKey:@"survives"]];

    if ([recent rangeOfString:@"http://"].location != NSNotFound ||
        [recent rangeOfString:@"https://"].location != NSNotFound) {
        //webView
        CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
        webVC.linkUrl = recent;
        [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/thelike"].location != NSNotFound) {
        //设置
        MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
        settingVC.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
        return;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/andlove"].location != NSNotFound) {
        //首页
        [AppTools getCurrentVC].tabBarController.selectedIndex = 0;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/standard"].location != NSNotFound) {
        //登录
        [AppTools toLogin];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/classes"].location != NSNotFound) {
        //订单
        [AppTools getCurrentVC].tabBarController.selectedIndex = 1;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/islove"].location != NSNotFound) {
        //产品详情
        NSArray *array = [recent componentsSeparatedByString:@"="];
        NSString *theId = [NSString stringWithFormat:@"%@",[array safeObjectAtIndex:1]];
        ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
        vc.productId = theId;
        vc.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:vc animated:YES];
        return;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
